<?php

/*============================================
Classe Iainstruments
============================================*/

class Iainstruments extends ModelObject{

	protected static $cached_object_list= array();
	protected $DEVICE_NAME;
	protected $DEVICE_LABEL;
	protected $MONITORING_STATUS;
	protected $DEVICE_TYPE;
	protected $SUB_SYSTEM;
	protected $MONITORING_DELAY;
	protected $ADRESS;
	protected $SECONDARY_ADRESS;
	protected $BUS_ID;
	protected $MEAS_NAME;
	protected $INSTRUMENT_TIMEOUT;
	protected $INST_LIE_COMM;
	protected $CHECK_END_DEVICE;
	protected $DEV_END_INDICATOR;
	protected $CONF_DEVICE;
	protected $FICTIF_DEVICE;

	protected function __construct( $DEVICE_NAME,$DEVICE_LABEL,$MONITORING_STATUS,$DEVICE_TYPE,$SUB_SYSTEM,$MONITORING_DELAY,$ADRESS,$SECONDARY_ADRESS,$BUS_ID,$MEAS_NAME,$INSTRUMENT_TIMEOUT,$INST_LIE_COMM,$CHECK_END_DEVICE,$DEV_END_INDICATOR,$CONF_DEVICE,$FICTIF_DEVICE){
		try{
			
			$this->DEVICE_NAME = $DEVICE_NAME;
			$this->DEVICE_LABEL = $DEVICE_LABEL;
			$this->MONITORING_STATUS = $MONITORING_STATUS;
			$this->DEVICE_TYPE = $DEVICE_TYPE;
			$this->SUB_SYSTEM = $SUB_SYSTEM;
			$this->MONITORING_DELAY = $MONITORING_DELAY;
			$this->ADRESS = $ADRESS;
			$this->SECONDARY_ADRESS = $SECONDARY_ADRESS;
			$this->BUS_ID = $BUS_ID;
			$this->MEAS_NAME = $MEAS_NAME;
			$this->INSTRUMENT_TIMEOUT = $INSTRUMENT_TIMEOUT;
			$this->INST_LIE_COMM = $INST_LIE_COMM;
			$this->CHECK_END_DEVICE = $CHECK_END_DEVICE;
			$this->DEV_END_INDICATOR = $DEV_END_INDICATOR;
			$this->CONF_DEVICE = $CONF_DEVICE;
			$this->FICTIF_DEVICE = $FICTIF_DEVICE;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getDEVICENAME(){
		return $this->DEVICE_NAME;
	}

	public function getDEVICELABEL(){
		return $this->DEVICE_LABEL;
	}

	public function getMONITORINGSTATUS(){
		return $this->MONITORING_STATUS;
	}

	public function getDEVICETYPE(){
		return $this->DEVICE_TYPE;
	}

	public function getSUBSYSTEM(){
		return $this->SUB_SYSTEM;
	}

	public function getMONITORINGDELAY(){
		return $this->MONITORING_DELAY;
	}

	public function getADRESS(){
		return $this->ADRESS;
	}

	public function getSECONDARYADRESS(){
		return $this->SECONDARY_ADRESS;
	}

	public function getBUSID(){
		return BUSID::get($this->BUS_ID);
	}

	public function getMEASNAME(){
		return $this->MEAS_NAME;
	}

	public function getINSTRUMENTTIMEOUT(){
		return $this->INSTRUMENT_TIMEOUT;
	}

	public function getINSTLIECOMM(){
		return $this->INST_LIE_COMM;
	}

	public function getCHECKENDDEVICE(){
		return $this->CHECK_END_DEVICE;
	}

	public function getDEVENDINDICATOR(){
		return $this->DEV_END_INDICATOR;
	}

	public function getCONFDEVICE(){
		return $this->CONF_DEVICE;
	}

	public function getFICTIFDEVICE(){
		return $this->FICTIF_DEVICE;
	}

	public function setDEVICELABEL( $DEVICE_LABEL){
		try{
			$temp = $this->DEVICE_LABEL;
			if($this->DEVICE_LABEL !== $DEVICE_LABEL){
				$this->DEVICE_LABEL = $DEVICE_LABEL;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->DEVICE_LABEL = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVICELABEL() a échouée",0,$e);
		}
	}

	public function setMONITORINGSTATUS( $MONITORING_STATUS){
		try{
			$temp = $this->MONITORING_STATUS;
			if($this->MONITORING_STATUS !== $MONITORING_STATUS){
				$this->MONITORING_STATUS = $MONITORING_STATUS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MONITORING_STATUS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMONITORINGSTATUS() a échouée",0,$e);
		}
	}

	public function setDEVICETYPE( $DEVICE_TYPE){
		try{
			$temp = $this->DEVICE_TYPE;
			if($this->DEVICE_TYPE !== $DEVICE_TYPE){
				$this->DEVICE_TYPE = $DEVICE_TYPE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->DEVICE_TYPE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVICETYPE() a échouée",0,$e);
		}
	}

	public function setSUBSYSTEM( $SUB_SYSTEM){
		try{
			$temp = $this->SUB_SYSTEM;
			if($this->SUB_SYSTEM !== $SUB_SYSTEM){
				$this->SUB_SYSTEM = $SUB_SYSTEM;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SUB_SYSTEM = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSUBSYSTEM() a échouée",0,$e);
		}
	}

	public function setMONITORINGDELAY( $MONITORING_DELAY){
		try{
			$temp = $this->MONITORING_DELAY;
			if($this->MONITORING_DELAY !== $MONITORING_DELAY){
				$this->MONITORING_DELAY = $MONITORING_DELAY;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MONITORING_DELAY = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMONITORINGDELAY() a échouée",0,$e);
		}
	}

	public function setADRESS( $ADRESS){
		try{
			$temp = $this->ADRESS;
			if($this->ADRESS !== $ADRESS){
				$this->ADRESS = $ADRESS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->ADRESS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setADRESS() a échouée",0,$e);
		}
	}

	public function setSECONDARYADRESS( $SECONDARY_ADRESS){
		try{
			$temp = $this->SECONDARY_ADRESS;
			if($this->SECONDARY_ADRESS !== $SECONDARY_ADRESS){
				$this->SECONDARY_ADRESS = $SECONDARY_ADRESS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SECONDARY_ADRESS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSECONDARYADRESS() a échouée",0,$e);
		}
	}

	public function setBUSID( $BUS_ID){
		try{
			$temp = $this->BUS_ID;
			if(is_null($BUS_ID)){
				throw new Exception("Le paramètre \$bUS_ID de type get_class($bUS_ID) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($BUS_ID instanceof BUSID) || is_null($BUS_ID->getId())){
				throw new Exception("Le paramètre \$bUS_ID de type get_class($bUS_ID) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->BUS_ID !== $BUS_ID->getId()){
					$this->BUS_ID = $BUS_ID->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->BUS_ID = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setBUSID() a échouée",0,$e);
		}
	}

	public function setMEASNAME( $MEAS_NAME){
		try{
			$temp = $this->MEAS_NAME;
			if($this->MEAS_NAME !== $MEAS_NAME){
				$this->MEAS_NAME = $MEAS_NAME;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->MEAS_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setMEASNAME() a échouée",0,$e);
		}
	}

	public function setINSTRUMENTTIMEOUT( $INSTRUMENT_TIMEOUT){
		try{
			$temp = $this->INSTRUMENT_TIMEOUT;
			if($this->INSTRUMENT_TIMEOUT !== $INSTRUMENT_TIMEOUT){
				$this->INSTRUMENT_TIMEOUT = $INSTRUMENT_TIMEOUT;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->INSTRUMENT_TIMEOUT = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setINSTRUMENTTIMEOUT() a échouée",0,$e);
		}
	}

	public function setINSTLIECOMM( $INST_LIE_COMM){
		try{
			$temp = $this->INST_LIE_COMM;
			if($this->INST_LIE_COMM !== $INST_LIE_COMM){
				$this->INST_LIE_COMM = $INST_LIE_COMM;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->INST_LIE_COMM = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setINSTLIECOMM() a échouée",0,$e);
		}
	}

	public function setCHECKENDDEVICE( $CHECK_END_DEVICE){
		try{
			$temp = $this->CHECK_END_DEVICE;
			if($this->CHECK_END_DEVICE !== $CHECK_END_DEVICE){
				$this->CHECK_END_DEVICE = $CHECK_END_DEVICE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->CHECK_END_DEVICE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCHECKENDDEVICE() a échouée",0,$e);
		}
	}

	public function setDEVENDINDICATOR( $DEV_END_INDICATOR){
		try{
			$temp = $this->DEV_END_INDICATOR;
			if($this->DEV_END_INDICATOR !== $DEV_END_INDICATOR){
				$this->DEV_END_INDICATOR = $DEV_END_INDICATOR;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->DEV_END_INDICATOR = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVENDINDICATOR() a échouée",0,$e);
		}
	}

	public function setCONFDEVICE( $CONF_DEVICE){
		try{
			$temp = $this->CONF_DEVICE;
			if($this->CONF_DEVICE !== $CONF_DEVICE){
				$this->CONF_DEVICE = $CONF_DEVICE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->CONF_DEVICE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setCONFDEVICE() a échouée",0,$e);
		}
	}

	public function setFICTIFDEVICE( $FICTIF_DEVICE){
		try{
			$temp = $this->FICTIF_DEVICE;
			if($this->FICTIF_DEVICE !== $FICTIF_DEVICE){
				$this->FICTIF_DEVICE = $FICTIF_DEVICE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->FICTIF_DEVICE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setFICTIFDEVICE() a échouée",0,$e);
		}
	}

	public static function create( $DEVICE_NAME,$DEVICE_LABEL,$MONITORING_STATUS,$DEVICE_TYPE,$SUB_SYSTEM,$MONITORING_DELAY,$ADRESS,$SECONDARY_ADRESS,$BUS_ID,$MEAS_NAME,$INSTRUMENT_TIMEOUT,$INST_LIE_COMM,$CHECK_END_DEVICE,$DEV_END_INDICATOR,$CONF_DEVICE,$FICTIF_DEVICE){
		try{
			$iainstruments = new Iainstruments($DEVICE_NAME, $DEVICE_LABEL, $MONITORING_STATUS, $DEVICE_TYPE, $SUB_SYSTEM, $MONITORING_DELAY, $ADRESS, $SECONDARY_ADRESS, $BUS_ID, $MEAS_NAME, $INSTRUMENT_TIMEOUT, $INST_LIE_COMM, $CHECK_END_DEVICE, $DEV_END_INDICATOR, $CONF_DEVICE, $FICTIF_DEVICE);
			$iainstruments->insert();
			Iainstruments::cacheObject($iainstruments);
			return $iainstruments;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Iainstruments a échouée",0,$e);
		}
	}

	public static function get( $DEVICE_NAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($DEVICE_NAME) || $DEVICE_NAME === "") {
				return null;
			}
			if(Iainstruments::existsInCache($DEVICE_NAME)) {
				return Iainstruments::getFromCache($DEVICE_NAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iainstruments` WHERE `DEVICE_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$DEVICE_NAME));
			if ($row = $stmt->fetch()){
				$iainstruments = new Iainstruments( $row['DEVICE_NAME'],$row['DEVICE_LABEL'],$row['MONITORING_STATUS'],$row['DEVICE_TYPE'],$row['SUB_SYSTEM'],$row['MONITORING_DELAY'],$row['ADRESS'],$row['SECONDARY_ADRESS'],$row['BUS_ID'],$row['MEAS_NAME'],$row['INSTRUMENT_TIMEOUT'],$row['INST_LIE_COMM'],$row['CHECK_END_DEVICE'],$row['DEV_END_INDICATOR'],$row['CONF_DEVICE'],$row['FICTIF_DEVICE']);
				$iainstruments->id =  $row['DEVICE_NAME'];
				$dbh->commit();
				Iainstruments::cacheObject($iainstruments);
				return $iainstruments;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $DEVICE_NAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iainstruments` SET `DEVICE_LABEL` = :DEVICE_LABEL, `MONITORING_STATUS` = :MONITORING_STATUS, `DEVICE_TYPE` = :DEVICE_TYPE, `SUB_SYSTEM` = :SUB_SYSTEM, `MONITORING_DELAY` = :MONITORING_DELAY, `ADRESS` = :ADRESS, `SECONDARY_ADRESS` = :SECONDARY_ADRESS, `BUS_ID` = :BUS_ID, `MEAS_NAME` = :MEAS_NAME, `INSTRUMENT_TIMEOUT` = :INSTRUMENT_TIMEOUT, `INST_LIE_COMM` = :INST_LIE_COMM, `CHECK_END_DEVICE` = :CHECK_END_DEVICE, `DEV_END_INDICATOR` = :DEV_END_INDICATOR, `CONF_DEVICE` = :CONF_DEVICE, `FICTIF_DEVICE` = :FICTIF_DEVICE WHERE `DEVICE_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':DEVICE_LABEL'=>$this->DEVICE_LABEL, ':MONITORING_STATUS'=>$this->MONITORING_STATUS, ':DEVICE_TYPE'=>$this->DEVICE_TYPE, ':SUB_SYSTEM'=>$this->SUB_SYSTEM, ':MONITORING_DELAY'=>$this->MONITORING_DELAY, ':ADRESS'=>$this->ADRESS, ':SECONDARY_ADRESS'=>$this->SECONDARY_ADRESS, ':BUS_ID'=>$this->BUS_ID, ':MEAS_NAME'=>$this->MEAS_NAME, ':INSTRUMENT_TIMEOUT'=>$this->INSTRUMENT_TIMEOUT, ':INST_LIE_COMM'=>$this->INST_LIE_COMM, ':CHECK_END_DEVICE'=>$this->CHECK_END_DEVICE, ':DEV_END_INDICATOR'=>$this->DEV_END_INDICATOR, ':CONF_DEVICE'=>$this->CONF_DEVICE, ':FICTIF_DEVICE'=>$this->FICTIF_DEVICE, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iainstruments` WHERE `DEVICE_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Iainstruments::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iainstruments` ( `DEVICE_LABEL`, `MONITORING_STATUS`, `DEVICE_TYPE`, `SUB_SYSTEM`, `MONITORING_DELAY`, `ADRESS`, `SECONDARY_ADRESS`, `BUS_ID`, `MEAS_NAME`, `INSTRUMENT_TIMEOUT`, `INST_LIE_COMM`, `CHECK_END_DEVICE`, `DEV_END_INDICATOR`, `CONF_DEVICE`, `FICTIF_DEVICE`) VALUES (:DEVICE_LABEL, :MONITORING_STATUS, :DEVICE_TYPE, :SUB_SYSTEM, :MONITORING_DELAY, :ADRESS, :SECONDARY_ADRESS, :BUS_ID, :MEAS_NAME, :INSTRUMENT_TIMEOUT, :INST_LIE_COMM, :CHECK_END_DEVICE, :DEV_END_INDICATOR, :CONF_DEVICE, :FICTIF_DEVICE)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':DEVICE_LABEL'=>$this->DEVICE_LABEL, ':MONITORING_STATUS'=>$this->MONITORING_STATUS, ':DEVICE_TYPE'=>$this->DEVICE_TYPE, ':SUB_SYSTEM'=>$this->SUB_SYSTEM, ':MONITORING_DELAY'=>$this->MONITORING_DELAY, ':ADRESS'=>$this->ADRESS, ':SECONDARY_ADRESS'=>$this->SECONDARY_ADRESS, ':BUS_ID'=>$this->BUS_ID, ':MEAS_NAME'=>$this->MEAS_NAME, ':INSTRUMENT_TIMEOUT'=>$this->INSTRUMENT_TIMEOUT, ':INST_LIE_COMM'=>$this->INST_LIE_COMM, ':CHECK_END_DEVICE'=>$this->CHECK_END_DEVICE, ':DEV_END_INDICATOR'=>$this->DEV_END_INDICATOR, ':CONF_DEVICE'=>$this->CONF_DEVICE, ':FICTIF_DEVICE'=>$this->FICTIF_DEVICE));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iainstruments`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Iainstruments::existsInCache($row['DEVICE_NAME'])){
					$obj = Iainstruments::getFromCache($row['DEVICE_NAME']);
				}
				else{
					$obj = new Iainstruments( $row['DEVICE_NAME'],$row['DEVICE_LABEL'],$row['MONITORING_STATUS'],$row['DEVICE_TYPE'],$row['SUB_SYSTEM'],$row['MONITORING_DELAY'],$row['ADRESS'],$row['SECONDARY_ADRESS'],$row['BUS_ID'],$row['MEAS_NAME'],$row['INSTRUMENT_TIMEOUT'],$row['INST_LIE_COMM'],$row['CHECK_END_DEVICE'],$row['DEV_END_INDICATOR'],$row['CONF_DEVICE'],$row['FICTIF_DEVICE']);
				}
				if(!is_null($obj)){
					$obj->id = $row['DEVICE_NAME'];
					Iainstruments::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iainstruments a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iainstruments`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iainstruments a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iainstruments`';
			$sql_select = 'SELECT DEVICE_NAME FROM `iainstruments`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Iainstruments::getObjectFromCache($row['DEVICE_NAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Iainstruments::removeObjectFromCache($row['DEVICE_NAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Iainstruments a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListIaantenna( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `ANT_ID_EPHEMERIS` FROM `iaantenna`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `DEVICE_NAME` = :id';
			}
			else{
				$sql .= $where.' AND `DEVICE_NAME` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Iaantenna::get($row['ANT_ID_EPHEMERIS']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Iaantenna liés à l'objet de type Iainstruments ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListIacommand( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `COMNAME` FROM `iacommand`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `DEVICE_NAME` = :id';
			}
			else{
				$sql .= $where.' AND `DEVICE_NAME` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Iacommand::get($row['COMNAME']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Iacommand liés à l'objet de type Iainstruments ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListIaswitchconf( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `LIB_CONF` FROM `iaswitchconf`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `DEVICE_NAME` = :id';
			}
			else{
				$sql .= $where.' AND `DEVICE_NAME` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = Iaswitchconf::get($row['LIB_CONF']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type Iaswitchconf liés à l'objet de type Iainstruments ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	/*fonction de jointure*/
	public function getListMcsParameters( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$objs = array();
			$sql = 'SELECT `PARAMETER_NAME` FROM `mcs_parameters`';
			$where = self::generateWhereClause($search_params);
			if($where === ""){
				$sql .= $where.' WHERE `DEVICE_NAME` = :id';
			}
			else{
				$sql .= $where.' AND `DEVICE_NAME` = :id';
			}
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$array[':id'] = $this->id;
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = McsParameters::get($row['PARAMETER_NAME']);
				if(!is_null($obj)){
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La recuperation de la liste d'objets de type McsParameters liés à l'objet de type Iainstruments ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

}